package com.yantu.controller.student;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yantu.bean.Message;
import com.yantu.bean.MessageUser;
import com.yantu.mapper.MessageMapper;
import com.yantu.mapper.MessageUserMapper;
import com.yantu.util.Result;
import com.yantu.view.student.StudentMessagePageView;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@ApiModel("消息分页接口信息")
public class StudentMessagePageController {

    @Autowired
    MessageMapper messageMapper;
    @Autowired
    MessageUserMapper messageUserMapper;


    @GetMapping("/messagePage/{StudentMessagePageView}")
    @ApiOperation("试卷分页信息")
    public Result studentPaper(@ModelAttribute("MessagePageView")StudentMessagePageView StudentMessagePageView) {
        PageHelper.startPage(StudentMessagePageView.getPageIndex(),StudentMessagePageView.getPageSize());

        List<MessageUser> messageUsers = messageUserMapper.selectList(null);
        if(messageUsers!=null) {
            PageInfo pageInfo = new PageInfo(messageUsers);
            List<MessageUser> messageUsers1 = pageInfo.getList();
            Map<String, Object> map = new HashMap<>();
            map.put("total", pageInfo.getTotal());
            List<Object> list1 = new ArrayList();
            for (MessageUser messageUser : messageUsers1) {
                Map<String, Object> map1 = new HashMap<>();
                map1.put("id",messageUser.getId());
                Message message = messageMapper.selectById(messageUser.getMessageId());
                map1.put("title",message.getTitle());
                map1.put("messageId",messageUser.getMessageId());
                map1.put("content",message.getContent());
                map1.put("readed",messageUser.getReaded());
                map1.put("createTime",messageUser.getCreateTime());
                map1.put("sendUserName",message.getSendUserName());
                list1.add(map1);
            }
            map.put("list", list1);
            return Result.succ(200, "查询成功", map);
        }

        else return Result.succ(200, "查询不到数据", null);
    }
}
